﻿using Microsoft.EntityFrameworkCore;
using ServerManager.Data;
using ServerManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ServerManager.Repository
{
    public class IpAddressRepository : EfRepository<IpAddress>, IIpAddressRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public IpAddressRepository(ApplicationDbContext dbContext)
            : base(dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<IEnumerable<IpAddressViewModel>> GetListAsync()
        {
            var ips = from i in _dbContext.IpAddresses
                      join s in _dbContext.Servers
                      on i.ServerId equals s.Id into temp
                      from t in temp.DefaultIfEmpty()
                      select new IpAddressViewModel
                      {
                          Id = i.Id,
                          Ip = i.Ip,
                          Status = i.Status,
                          Remark = i.Remark,
                          ServerId = i.ServerId,
                          ServerPosition = t==null?"":t.Position
                      };
            return await ips.OrderBy(x => x.Ip).ToListAsync();
        }
        
    }
}
